package com.zsh.fmmall.service.impl;

import com.zsh.fmmall.dao.ProductImgMapper;
import com.zsh.fmmall.dao.ProductMapper;
import com.zsh.fmmall.dao.ProductSkuMapper;
import com.zsh.fmmall.entity.Product;
import com.zsh.fmmall.entity.ProductImg;
import com.zsh.fmmall.entity.ProductSku;
import com.zsh.fmmall.entity.ProductVO;
import com.zsh.fmmall.service.ProductService;
import com.zsh.fmmall.vo.BaseCode;
import com.zsh.fmmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImgMapper productImgMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> productVOS = productMapper.selectRecommendProducts();
        if(productVOS.size()==0){
//            System.out.println(indexImgs.size());
            return new ResultVO(BaseCode.no,"fasle失败",null);
        }else {
            return new ResultVO(BaseCode.ok,"success成功",productVOS);
        }
    }

    /**
     * 1、 @Transactional 是声明式事务管理 编程中使用的注解
     * 2、 数据库事务默认的隔离级别是可重复读：保证数据的一致性
     * 查询商品详情的基本信息：因为都是单表查询，所以直接用tkmapper提供的方法就可以了
     * @param productId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO getProductBasicInfo(String productId) {
        //1、商品的基本信息
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        criteria.andEqualTo("productStatus",1);//状态1表示上架商品
        List<Product> products = productMapper.selectByExample(example);
        if(products.size()>0){
            //2、商品图片
            Example example1 = new Example(ProductImg.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("itemId",productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(example1);
            //3、商品套餐
            Example example2 = new Example(ProductSku.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("productId",productId);
            criteria2.andEqualTo("status",1);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(example2);
            //因为我要把商品、图片、套餐都返回到前端，而直接返回ResultVO已经不能够满足我们的需求，则可以把信息封装到集合中传输
            HashMap<String, Object> productInfo = new HashMap<>();
            productInfo.put("product",products);
            productInfo.put("productImgs",productImgs);
            productInfo.put("productSkus",productSkus);
            return new ResultVO(BaseCode.ok,"success",productInfo);
        }
            return new ResultVO(BaseCode.no,"false",null);
    }
}
